#!/usr/bin/env python

from pgap.Chromosone import Chromosone
from pgap.impl.FloatGene import FloatGene

class CubicSolverChromosone(Chromosone):
	
	def __init__(self,a,b,c,d):
		self.a = a
		self.b = b
		self.c = c
		self.d = d
		self.genes = list()
		for i in range(0,3):
			self.genes.append(FloatGene())
	
	def setGeneBounds(self,lowerBound,upperBound):
		self.genes[0].setBounds(lowerBound,upperBound)
		self.genes[1].setBounds(lowerBound,upperBound)
		self.genes[2].setBounds(lowerBound,upperBound)

	def evaluate(self,x):
		return self.a*(x**3) + self.b*(x**2) + self.c*x + self.d 

	def fitness(self):
		val_x1 = abs(self.evaluate(self.genes[0].getValue()))	
		val_x2 = abs(self.evaluate(self.genes[1].getValue()))	
		val_x3 = abs(self.evaluate(self.genes[2].getValue()))

		return (val_x1 + val_x2 + val_x3)	

	def clone(self):
		cloneChromosone = CubicSolverChromosone(self.a,self.b,\
					self.c,self.d)
		for gene in self.genes:
			cloneGene = gene.clone()
			cloneChromosone.addGene(gene)
		return cloneChromosone

	def __repr__(self):
		str = "<%f,%f,%f,%f>" % (self.fitness(),\
			self.genes[0].getValue(),\
			self.genes[1].getValue(),\
			self.genes[2].getValue())

		return str

	def __str__(self):
		str = "<%f,%f,%f,%f>" % (self.fitness(),\
				self.genes[0].getValue(),\
				self.genes[1].getValue(),
				self.genes[2].getValue())
		return str
